#!/bin/bash

system=$2
javaHome=$3
tomcatHome=$4
httpPort=$5

export JAVA_HOME=${javaHome}
export PATH=$PATH:${javaHome}/bin
export CATALINA_BASE=${tomcatHome}
export HTTP_PORT=${httpPort}

# Source function library. centos
if [ ${system} = "centos" ]
then
. /etc/rc.d/init.d/functions
fi

# ubuntu
if [ ${system} = "ubuntu" ]
then
. /lib/lsb/init-functions
fi

function check_permisson(){
         illegal_user_perm=(`find "${tomcatHome}" ! -user beetle -o ! -group beetle`)
         tomcat_pid=`netstat -tlnp 2>/dev/null |grep -w "${HTTP_PORT}"|awk '{print $7}'|awk -F '/' '{print $1}'`
         if [ ${#illegal_user_perm[*]} -ne 0 ];then
            echo -e "Some dirs or files permisson are uncorrect!please modify it:"
            echo "${illegal_user_perm[*]}" |xargs -n1
            return 1
         fi
         if [[ -n ${tomcat_pid} ]];then
            tomcat_user=`ps -ef |grep -w ${tomcat_pid} |grep -v grep |awk '{print $1}'`
               if [ "${tomcat_user}" != "beetle" ];then
                  echo "tomcat daemon  user is not beetle!${tomcat_user}"
                  return 2
               fi
         fi
}

function start(){
        check_permisson
        if [ "$?" -ne 0 ];then exit 1;fi
        tomcat_pid=`netstat -tlnp 2>/dev/null |grep -w "${HTTP_PORT}"|awk '{print $7}'|awk -F '/' '{print $1}'`
        if [ "${tomcat_pid}" ] ;then
        echo "tomcat service is running:${tomcat_pid}"
        else
        eval "rm -rf ${tomcatHome}/work/Catalina/localhost/* &>/dev/null"
        eval "sh ${tomcatHome}/bin/startup.sh &>/dev/null"
        echo "Start Tomcat complete ok"
        fi
        }
function killTomcat(){
        check_permisson
        if [ "$?" -ne 0 ];then exit 1;fi
        tomcat_pid=`netstat -tlnp 2>/dev/null |grep -w "${HTTP_PORT}"|awk '{print $7}'|awk -F '/' '{print $1}'`
        if [  "${tomcat_pid}" ];then
        eval "kill -9 ${tomcat_pid}"
        echo "Kill Tomcat complete ok"
        else
        echo "tomcat service is not running!"
        fi
        }
function stop(){
        check_permisson
        if [ "$?" -ne 0 ];then exit 1;fi
        tomcat_pid=`netstat -tlnp 2>/dev/null |grep -w "${HTTP_PORT}"|awk '{print $7}'|awk -F '/' '{print $1}'`
        if [  "${tomcat_pid}" ];then
        eval "sh ${tomcatHome}/bin/shutdown.sh &>/dev/null"
        echo "Shutdown Tomcat complete ok"
        else
        echo "tomcat service is not running!"
        fi
        }
function restart(){
       stop
       sleep 2
       start
       }
function status(){
        tomcat_pid=`netstat -tlnp 2>/dev/null |grep -w "${HTTP_PORT}"|awk '{print $7}'|awk -F '/' '{print $1}'`
        if [ "${tomcat_pid}" ] ;then
        echo "tomcat service is running :${tomcat_pid}"
        else
        echo "tomcat service is not running"
        fi
}


case "$1" in
    start)
        start ;;
    stop)
        stop ;;
    restart)
        restart ;;
    status)
        status ;;
    check)
        check_permisson ;;
    kill)
        killTomcat ;;
    *)
    echo "Usage: tomcat.sh {start|stop|restart|status|check|kill}"
    exit 1
esac
exit 0